//
//  YHButton.m
//  HTP
//
//  Created by admin on 15/10/29.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "YHButton.h"
static NSMutableDictionary *_groupButtonDic = nil;
@interface YHButton ()
@property (nonatomic,retain) UIImageView    *imageView;
@property (nonatomic,retain) UILabel        *titleLabel;
@property (nonatomic,retain) UIButton       *backBtn;
@property (nonatomic,assign) CGRect         selfFrame;

@property (nonatomic,retain) NSString       *selectImage;
@property (nonatomic,retain) NSString       *unSelectImage;

@property (nonatomic,retain) NSString       *title;
@end
@implementation YHButton
@synthesize delegate = _delegate;
@synthesize isSelect = _isSelect;
-(id)initWithDelegate:(id)delegate groupId:(NSString *)groupId buttonImage:(NSString *)image unSelectImage:(NSString *)unSelectImage title:(NSString *)title withFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    _selfFrame = frame;
    _selectImage = image;
    _unSelectImage = unSelectImage;
    _title = title;
    if (self) {
        [self initView];
        _delegate = delegate;
        _groupId = [groupId copy];
        [_backBtn addTarget:self action:@selector(YHBtnChecked) forControlEvents:UIControlEventTouchUpInside];
        [self addToGroup];
    }
    return self;
}
-(void)setBackSelected{
    _backBtn.selected = YES;
    self.isSelect = YES;
}
//TODO:单例模式记录添加的view
-(void)addToGroup{
    if (!_groupButtonDic) {
        _groupButtonDic = [[NSMutableDictionary dictionary] retain];
    }
    NSMutableArray *_btns = [_groupButtonDic objectForKey:_groupId];
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    [_btns addObject:self];
    [_groupButtonDic setObject:_btns forKey:_groupId];
}
/**
 *  对应button移除
 */
-(void)removeFromGroup{
    if (_groupButtonDic) {
        NSMutableArray *_btns = [_groupButtonDic objectForKey:_groupId];
        if (_btns) {
            [_btns removeObject:self];
            if (_btns.count == 0) {
                [_groupButtonDic removeObjectForKey:_groupId];
            }
        }
    }
}
//TODO:选中状态修改
/**
 *  初始化这个view
 */
-(void)initView{
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_selfFrame.size.width  / 4.0 - 22.5, 12, 25, 25)];
    _imageView.backgroundColor = [UIColor clearColor];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    if (self.isSelect) {
        [_imageView setImage:[UIImage imageNamed:_selectImage]];
    }else{
        [_imageView setImage:[UIImage imageNamed:_unSelectImage]];
    }
    [self addSubview:_imageView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageView.frame.size.width+_imageView.frame.origin.x + 10, 0, _selfFrame.size.width / 2.0, _selfFrame.size.height)];
    _titleLabel.textAlignment = _TEXT_ALIGN_CENTER;
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.text = _title;
    [self addSubview:_titleLabel];
    
    _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _selfFrame.size.width, _selfFrame.size.height)];
    [_backBtn setBackgroundColor:[UIColor clearColor]];
    [self addSubview:_backBtn];
    [self bringSubviewToFront:_backBtn];
}
/**
 * self 被设置为选中状态
 */
-(void)YHBtnChecked{
    if ( _delegate && [_delegate respondsToSelector:@selector(didSelectYHButton:groupId:)]) {
        [_delegate didSelectYHButton:self groupId:_groupId];
//        [self setSelectState];
    }
//    if (_isSelect) {
//        return;
//    }
//    _backBtn.selected = !_backBtn.selected;
//    _isSelect = _backBtn.selected;
//    
//    if (_backBtn.selected) {
//        [self yHUnCheckOtherView];
//        //把其他group 设置为非选择状态
//    }
//    
//    if (_backBtn.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectYHButton:groupId:)]) {
//        [_delegate didSelectYHButton:self groupId:_groupId];
//        [self setSelectState];
//    }
    
}
/**
 *  同组内修改选中状态
 */
-(void)yHUnCheckOtherView{
    NSMutableArray *btns = [_groupButtonDic objectForKey:_groupId];
    if (btns!=nil   &&  btns.count > 0) {
        [btns enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            YHButton *yhBtn = (YHButton *)obj;
            if (yhBtn.isSelect && ![yhBtn isEqual:self]) {
                yhBtn.isSelect = NO;
            }
        }];
    }
}
/**
 *
 *  记录self 是否被选中
 *  @param isSelect
 */
-(void)setIsSelect:(BOOL)isSelect{
    if (_isSelect == isSelect) {
        return;
    }
    _isSelect = isSelect;
    _backBtn.selected = _isSelect;
    
    if (_backBtn.selected) {
        [self yHUnCheckOtherView];
    }else{
        [self setUnSelectState];
    }
    
    if (_backBtn.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectYHButton:groupId:)]) {
        [_delegate didSelectYHButton:self groupId:_groupId];
        [self setSelectState];
    }
    
}
//TODO:设置UIView 的选中状态
-(void)setSelectState{
    [_imageView setImage:[UIImage imageNamed:_selectImage]];
}
-(void)setUnSelectState{
    [_imageView setImage:[UIImage imageNamed:_unSelectImage]];
    [_imageView setBackgroundColor:[UIColor whiteColor]];
}
//TODO:非arc
-(void)dealloc{
    [self removeFromGroup];
    
    _delegate = nil;
    [_groupId release];
    _groupId = nil;
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
